import { ref } from 'vue'
import { adapter, request } from '@/request'
import is from '@/utils/is'
import { message } from 'ant-design-vue'
import { getKeywordRankingList as getKeywordRankingListApi } from '@/core/api/analysis'
import type { RequestWrapper } from '@/core/interfaces/common'

// 关键词排名数据接口
export interface KeywordRankingItem {
  keyword: string
  ranking: number
  createTime: string
  accountName: string // 账号名称
}

export interface KeywordRankingResponse {
  totalSize: number
  keywords: KeywordRankingItem[]
}

export default function getKeywordRankingList() {
  const service = adapter<RequestWrapper<KeywordRankingResponse>>(request)
  const loading = ref(false)
  const response = ref<Partial<KeywordRankingResponse>>({})

  const fetch = (params: {
    orderId: string | number
    timeRange: string | number
    pageNum: number
    pageSize: number
  }) => {
    const { url, method } = getKeywordRankingListApi()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        if (is.error(err)) {
          message.error(err.message || '获取关键词排名数据失败')
          response.value = {}
        }
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
